package isd.com.smarthome.adapter;

import isd.com.smarthome.model.*;
import isd.com.smarthome.provider.SmartHomeProvider;
import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorTreeAdapter;

public class RoomAdapter extends CursorTreeAdapter {
	private Context mcontext;
	private String mquery;
	
	public RoomAdapter(Cursor cursor, Context context, String query) {
		super(cursor, context);
		// TODO Auto-generated constructor stub
		this.mcontext = context;
		this.mquery = query;
	}
	
	@Override
	protected Cursor getChildrenCursor(Cursor groupCursor) {
		// TODO Auto-generated method stub
		Room room = new Room();
		SmartHomeProvider.getRoom(groupCursor, room);
		Cursor data =mcontext.getContentResolver().query(SmartHomeProvider.SWITCHES_CONTENT_URI,null,
				SmartHomeProvider.SwitchTable.ID_ROOM+"="+room.getIdRoom() + mquery , null, null);		
				
		return data;
	}
	@Override
	protected void bindChildView(View view, Context context, Cursor cursor,	boolean isLastChild) {
		// TODO Auto-generated method stub		
		Switches switches = new Switches();
		SmartHomeProvider.getSwitches(cursor, switches);
		RoomChild child = (RoomChild) view;	
		child.tv_roomchild.setText(switches.getNameSwitch());
//		child.setTag(switches);

	}

	@Override
	protected void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded) {
		// TODO Auto-generated method stub
		Room room = new Room();
		SmartHomeProvider.getRoom(cursor, room);
		RoomGroup group = (RoomGroup) view;
		
		group.tv_roomgroup.setText(room.getNameRoom());
		
	}



	@Override
	protected View newChildView(Context context, Cursor cursor,	boolean isLastChild, ViewGroup parent) {
		// TODO Auto-generated method stub
		RoomChild child = new RoomChild(context);
		return child;
	}

	@Override
	protected View newGroupView(Context context, Cursor cursor,	boolean isExpanded, ViewGroup parent) {
		// TODO Auto-generated method stub
		RoomGroup group = new RoomGroup(context);
		return group;
	}


}